/**
 * @Description: 统计对比逻辑
 * @Author BPF
 * @create 2021/12/9 1:06
 */
import { Type, MathUtils } from '@bianpengfei/utils'
import { calSummaryList } from './common'
import dayjs from 'dayjs'

const { Decimal } = MathUtils

// 根据list 返回所有数据各个总和
export function sumXAxis(list) {
  const tempSummaryList = list.filter(v => Array.isArray(v.__value) && v.__value.length).map(v => v.__valueSummary)
  if (tempSummaryList.length) {
    const [item0] = tempSummaryList || []
    const item0Clone = JSON.parse(JSON.stringify(item0))
    const keysMap = Object.keys(item0)
    keysMap.forEach(key => {
      // 判断是键值是否是数字型或fields存在key且值是数字型
      if (Type.isNumber(item0Clone[key])) {
        item0Clone[key] = 0
      }
    })
    return tempSummaryList.reduce((pre, cur, i) => {
      keysMap.forEach(k => {
        if (Type.isNumber(cur[k])) {
          pre[k] = +new Decimal(+pre[k]).add(+cur[k]).toFixed()
        }
      })
      return pre
    }, item0Clone)
  }
  return {}
}

/**
 * 处理日
 * @param list
 * @param _listObj
 * @param params
 * @returns {{xAxisUnitsPre: *[], summary: {}, xAxisUnits: *[], xAxis: {name: *, alias: *, __value: [(*&{__PID: *})]|[], __valueSummary: {}, id: *}[], xAxisSummary: {}, xAxisSummaryPre: {}, summaryPre: {}, xAxisPre: {name: *, alias: *, __value: [(*&{__PID: *})]|[], __valueSummary: {}, id: *}[]}}
 */
export function handlerDay(list = [], _listObj = {}, params = {}) {
  let xAxis = [],
    xAxisPre = [],
    xAxisUnits = [],
    xAxisUnitsPre = [],
    xAxisSummary = {},
    xAxisSummaryPre = {},
    summary = {},
    summaryPre = {}
  const { start, lastStart } = params
  const tempListCur = _listObj[start] ? [{ ..._listObj[start], __PID: start }] : []
  const callListCur = calSummaryList({ list: tempListCur, primaryKey: '__PID' })
  summary = callListCur?.fieldsObj

  const tempListPre = _listObj[start] ? [{ ..._listObj[lastStart], __PID: start }] : []
  const callListPre = calSummaryList({ list: tempListPre, primaryKey: '__PID' })
  summaryPre = callListPre?.fieldsObj

  xAxisUnits = [...new Array(7)]
    .map((v, vi) => {
      return dayjs(start).subtract(vi, 'day').format('YYYY-MM-DD')
    })
    .reverse()
  xAxis = xAxisUnits.map((v, vi) => {
    const tempList = _listObj[v] ? [{ ..._listObj[v], __PID: v }] : []
    const callList = calSummaryList({ list: tempList, primaryKey: '__PID' })
    return {
      id: vi,
      name: v,
      alias: v,
      __value: list.filter(o => o.date == v),
      __valueSummary: callList?.fieldsObj
    }
  })
  if (xAxis.length) {
    xAxisSummary = sumXAxis(xAxis)
  }

  xAxisUnitsPre = [...new Array(7)]
    .map((v, vi) => {
      return dayjs(start)
        .subtract(vi + 7, 'day')
        .format('YYYY-MM-DD')
    })
    .reverse()
  xAxisPre = xAxisUnitsPre.map((v, vi) => {
    const tempList = _listObj[v] ? [{ ..._listObj[v], __PID: v }] : []
    const callList = calSummaryList({ list: tempList, primaryKey: '__PID' })
    return {
      id: vi,
      name: v,
      alias: v,
      __value: list.filter(o => o.date == v),
      __valueSummary: callList?.fieldsObj
    }
  })
  if (xAxisPre.length) {
    xAxisSummaryPre = sumXAxis(xAxis)
  }
  return {
    xAxis,
    xAxisPre,
    xAxisUnits,
    xAxisUnitsPre,
    xAxisSummary,
    xAxisSummaryPre,
    summary,
    summaryPre
  }
}

/**
 * 处理月
 * @param list
 * @param _listObj
 * @param params
 * @returns {{xAxisUnitsPre: *[], summary: {}, xAxisUnits: *[], xAxis: {name: *, alias: *, __value: [(*&{__PID: *})]|[], __valueSummary: {}, id: *}[], xAxisSummary: {}, xAxisSummaryPre: {}, summaryPre: {}, xAxisPre: {name: *, alias: *, __value: [(*&{__PID: *})]|[], __valueSummary: {}, id: *}[]}}
 */
export function handlerMonth(list = [], _listObj = {}, params = {}) {
  let xAxis = [],
    xAxisPre = [],
    xAxisChain = [], // 环比数据
    xAxisUnits = [],
    xAxisUnitsPre = [],
    xAxisUnitsChain = {},
    xAxisSummary = {},
    xAxisSummaryPre = {},
    xAxisSummaryChain = {},
    summary = {},
    summaryPre = {}
  const { start, lastStart, chainMonthStart } = params
  const daysInMonth = dayjs(start.slice(0, 7)).daysInMonth()
  xAxisUnits = [...new Array(daysInMonth)].map((v, vi) => {
    return dayjs(start).add(vi, 'day').format('YYYY-MM-DD')
  })
  xAxis = xAxisUnits.map((v, vi) => {
    const tempList = _listObj[v] ? [{ ..._listObj[v], __PID: v }] : []
    const callList = calSummaryList({ list: tempList, primaryKey: '__PID' })
    return {
      id: vi,
      name: v,
      alias: v,
      __value: list.filter(o => o.date == v),
      __valueSummary: callList?.fieldsObj
    }
  })
  if (xAxis.length) {
    summary = xAxisSummary = sumXAxis(xAxis)
  }

  const daysInMonthPre = dayjs(lastStart.slice(0, 7)).daysInMonth()
  xAxisUnitsPre = [...new Array(daysInMonthPre)].map((v, vi) => {
    return dayjs(lastStart).add(vi, 'day').format('YYYY-MM-DD')
  })
  xAxisPre = xAxisUnitsPre.map((v, vi) => {
    const tempList = _listObj[v] ? [{ ..._listObj[v], __PID: v }] : []
    const callList = calSummaryList({ list: tempList, primaryKey: '__PID' })
    return {
      id: vi,
      name: v,
      alias: v,
      __value: list.filter(o => o.date == v),
      __valueSummary: callList?.fieldsObj
    }
  })
  if (xAxisPre.length) {
    summaryPre = xAxisSummaryPre = sumXAxis(xAxisPre)
  }

  let chainMonthStartFinal = chainMonthStart || dayjs(start).subtract(1, 'year').format('YYYY-MM-DD')
  const daysInMonthChain = dayjs(chainMonthStartFinal?.slice(0, 7)).daysInMonth()
  xAxisUnitsChain = [...new Array(daysInMonthChain)].map((v, vi) => {
    return dayjs(chainMonthStartFinal).add(vi, 'day').format('YYYY-MM-DD')
  })
  xAxisChain = xAxisUnitsChain.map((v, vi) => {
    const tempList = _listObj[v] ? [{ ..._listObj[v], __PID: v }] : []
    const callList = calSummaryList({ list: tempList, primaryKey: '__PID' })
    return {
      id: vi,
      name: v,
      alias: v,
      __value: list.filter(o => o.date == v),
      __valueSummary: callList?.fieldsObj
    }
  })
  if (xAxisChain.length) {
    xAxisSummaryChain = sumXAxis(xAxisChain)
  }

  return {
    xAxis,
    xAxisPre,
    xAxisChain,
    xAxisUnits,
    xAxisUnitsPre,
    xAxisUnitsChain,
    xAxisSummary,
    xAxisSummaryPre,
    xAxisSummaryChain,
    summary,
    summaryPre
  }
}

/**
 * 处理季节
 * @param list
 * @param _listObj
 * @param params
 * @returns {{xAxisUnitsPre: *[], summary: {}, xAxisUnits: *[], xAxis: {name: *, alias: *, __value: [(*&{__PID: *})]|[], __valueSummary: {}, id: *}[], xAxisSummary: {}, xAxisSummaryPre: {}, summaryPre: {}, xAxisPre: {name: *, alias: *, __value: [(*&{__PID: *})]|[], __valueSummary: {}, id: *}[]}}
 */
export function handlerQuarter(list = [], _listObj = {}, params = {}) {
  let xAxis = [],
    xAxisPre = [],
    xAxisUnits = [],
    xAxisUnitsPre = [],
    xAxisSummary = {},
    xAxisSummaryPre = {},
    summary = {},
    summaryPre = {}
  const { start, lastStart } = params
  const quarterArr = {
    1: [1, 2, 3],
    2: [4, 5, 6],
    3: [7, 8, 9],
    4: [10, 11, 12]
  }
  let year = start.slice(0, 4)
  const month = start.slice(5, 7)
  xAxisUnits = Object.values(quarterArr)
    .find(v => v.includes(+month))
    .map(v => `${year}-${v.toString().padStart(2, '0')}`)

  xAxis = xAxisUnits.map((v, vi) => {
    const tempList = list.filter(o => v == o?.date?.slice(0, 7)).map(o => ({ ...o, __PID: v }))
    const callList = calSummaryList({ list: tempList, primaryKey: '__PID' })
    return {
      id: vi,
      name: v,
      alias: v && `${+String(v).slice(5, 7)}月`,
      __value: tempList,
      __valueSummary: callList?.fieldsObj
    }
  })
  if (xAxis.length) {
    summary = xAxisSummary = sumXAxis(xAxis)
  }

  let yearPre = lastStart.slice(0, 4)
  let monthPre = lastStart.slice(5, 7)
  xAxisUnitsPre = Object.values(quarterArr)
    .find(v => v.includes(+monthPre))
    .map(v => `${yearPre}-${v.toString().padStart(2, '0')}`)

  xAxisPre = xAxisUnitsPre.map((v, vi) => {
    const tempList = list.filter(o => v == o?.date?.slice(0, 7)).map(o => ({ ...o, __PID: v }))
    const callList = calSummaryList({ list: tempList, primaryKey: '__PID' })
    return {
      id: vi,
      name: v,
      alias: v && `${+String(v).slice(5, 7)}月`,
      __value: tempList,
      __valueSummary: callList?.fieldsObj
    }
  })
  if (xAxisPre.length) {
    summaryPre = xAxisSummaryPre = sumXAxis(xAxisPre)
  }
  return {
    xAxis,
    xAxisPre,
    xAxisUnits,
    xAxisUnitsPre,
    xAxisSummary,
    xAxisSummaryPre,
    summary,
    summaryPre
  }
}

/**
 * 处理年
 * @param list
 * @param _listObj
 * @param params
 * @returns {{xAxisUnitsPre: string[], summary: {}, xAxisUnits: string[], xAxis: {name: *, alias, __value: (*&{__PID: *})[], __valueSummary: {}, id: *}[], xAxisSummary: {}, xAxisSummaryPre: {}, summaryPre: {}, xAxisPre: {name: *, alias, __value: (*&{__PID: *})[], __valueSummary: {}, id: *}[]}}
 */
export function handlerYear(list = [], _listObj = {}, params = {}) {
  let xAxis = [],
    xAxisPre = [],
    xAxisUnits = [],
    xAxisUnitsPre = [],
    xAxisSummary = {},
    xAxisSummaryPre = {},
    summary = {},
    summaryPre = {}
  const { start, lastStart } = params
  let year = start.slice(0, 4)
  xAxisUnits = [...new Array(12)].map((v, vi) => `${year}-${String(vi + 1).padStart(2, '0')}`)
  xAxis = xAxisUnits.map((v, vi) => {
    const tempList = list.filter(o => v == o?.date?.slice(0, 7)).map(o => ({ ...o, __PID: v }))
    const callList = calSummaryList({ list: tempList, primaryKey: '__PID' })
    return {
      id: vi,
      name: v,
      alias: v && `${+String(v).slice(5, 7)}月`,
      __value: tempList,
      __valueSummary: callList?.fieldsObj
    }
  })
  if (xAxis.length) {
    summary = xAxisSummary = sumXAxis(xAxis)
  }

  let yearPre = lastStart.slice(0, 4)
  xAxisUnitsPre = [...new Array(12)].map((v, vi) => `${yearPre}-${String(vi + 1).padStart(2, '0')}`)
  xAxisPre = xAxisUnitsPre.map((v, vi) => {
    const tempList = list.filter(o => v == o?.date?.slice(0, 7)).map(o => ({ ...o, __PID: v }))
    const callList = calSummaryList({ list: tempList, primaryKey: '__PID' })
    return {
      id: vi,
      name: v,
      alias: v && `${+String(v).slice(5, 7)}月`,
      __value: tempList,
      __valueSummary: callList?.fieldsObj
    }
  })
  if (xAxisPre.length) {
    summaryPre = xAxisSummaryPre = sumXAxis(xAxisPre)
  }

  return {
    xAxis,
    xAxisPre,
    xAxisUnits,
    xAxisUnitsPre,
    xAxisSummary,
    xAxisSummaryPre,
    summary,
    summaryPre
  }
}
